<?php

    class CustomerController extends AdminController
    {
        public function init(){
            parent::init();
            $this->menu_parent_selected = 'customer';

        }

        /**
        * @return array action filters
        */
        public function filters()
        {
            return array(
                'accessControl', // perform access control for CRUD operations
                'postOnly + delete', // we only allow deletion via POST request
            );
        }

        /**
        * Specifies the access control rules.
        * This method is used by the 'accessControl' filter.
        * @return array access control rules
        */
        public function accessRules()
        {
            return array(
                array('allow',
                    'actions'=>array('view'),
                    'users'=>array('*'),
                ),
                array('allow', // allow authenticated user to perform 'create' and 'update' actions
                    'actions'=>array('index', 'update'),
                    'users'=>array('@'),
                ),
                array('deny',  // deny all users
                    'users'=>array('*'),
                ),
            );
        }

        public function actionIndex()
        {
            $this->menu_child_selected = 'customer_index';
            $this->menu_sub_selected = 'customer_index';

            $model = new Customer('search');
            $model->unsetAttributes();  // clear any default values
            if(isset($_GET['Customer'])) $model->attributes=$_GET['Customer'];


            $criteria=new CDbCriteria;

            $sort = Yii::app()->request->getQuery('Customer_sort');

            if(!$sort && $sort != 'id') $criteria->order = 't.id DESC'; 
            $criteria->compare('t.id',$model->id,true);
            $criteria->compare('t.phone',$model->phone,true);
            $criteria->compare('t.name',$model->name,true);
            $criteria->compare('t.created',$model->created,true);
            $criteria->compare('t.point',$model->point,true);
            $criteria->compare('t.status',$model->status);
            $criteria->compare('t.is_receive_sms',$model->is_receive_sms);

            $dataProvider = new CActiveDataProvider($model, array(
                'criteria'=>$criteria,
                'pagination'=>array(
                    'pageSize'=>20,
                ),
            ));

            $this->render('index',array(
                'model'         =>  $model,
                'dataProvider'         =>  $dataProvider,
            ));
        }

        /**
        * Updates a particular model.
        * If update is successful, the browser will be redirected to the 'view' page.
        * @param integer $id the ID of the model to be updated
        */
        public function actionUpdate($id)
        {
            $this->menu_child_selected = 'customer_update';
            $this->menu_sub_selected = 'customer_update';

            $model = Customer::model()->findByPk($id);
            $model->scenario = 'update';

            if(isset($_POST['Customer']))
            {    
                $post = Yii::app()->request->getPost('Customer');
                $oldPassword = $model->password;
                $model->attributes=$post;
                if($post['password']){
                    $model->password = $model->getHashPassword($model->password);    
                }else{
                    $model->password = $oldPassword;
                }

                if($model->validate()) {

                    $model->setIsNewRecord(FALSE);
                    $model->update();

                    Yii::app()->user->setFlash('success', "Cập nhật thông tin khách hàng {$model->phone} thành công!");
                    $this->refresh();
                }
            }

            $this->render('form',array(
                'model'=>$model,
            ));
        }



        /**
        * Returns the data model based on the primary key given in the GET variable.
        * If the data model is not found, an HTTP exception will be raised.
        * @param integer the ID of the model to be loaded
        */
        public function loadModel($id)
        {
            $model=Post::model()->findByPk($id);
            if($model===null)
                throw new CHttpException(404,'The requested page does not exist.');
            return $model;
        }

    }
